package com.bw.lib_net.test_okhttp

import okhttp3.Call
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * @author zhy
 * @date 2025-09-17 08:24
 * @description
 */
class TestOkHttp {
    fun test() {
        //1.创建OkHttpClient对象
        val okHttpClient = OkHttpClient.Builder().build()//构建者模式
        //2.创建Request对象
        val request = Request.Builder()
            .get()
            .url("https://www.baidu.com")
            .build()
        //3.client发起request ---> call
        val newCall = okHttpClient.newCall(request)
        //4.同步--->
        val response = newCall.execute()
        println(response)
        //5.异步--->
        newCall.enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
            }

            override fun onResponse(call: Call, response: Response) {
                //response结果
                val body = response.body
                val string = body?.string()
                println(string)
            }

        })
    }
}